package com.employee.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.employee.controller.vo.JsonResult;
import com.employee.dao.mapper.DepartmentMapper;
import com.employee.dao.mapper.EmployeeMapper;
import com.employee.dao.model.Department;
import com.employee.utils.page.PageView;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import tk.mybatis.mapper.entity.Example;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * 部门管理接口
 */
@RestController
@RequestMapping("/dept")
public class AdminDepartmentApi {

    private Logger logger = Logger.getLogger(AdminDepartmentApi.class.getName());

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //注册自定义的编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 查询部门列表
     * @param request
     * @return
     */
    @RequestMapping("list")
    public JsonResult deptList(@RequestBody JSONObject request)  {
        JsonResult result = new JsonResult(true, 200, "查询成功");
        logger.info("查询部门列表");
        PageView<Department> page = new PageView<>();
        page = page.startPage(request);
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        String kw = request.getString("kw");
        if(StringUtils.isNotBlank(kw)){
            criteria.andLike("deptName", "%"+kw+"%");
        }
        List<Department> list = departmentMapper.selectByExampleAndRowBounds(example, new RowBounds(page.getFirstResult(), page.getMaxresult()));
        for(Department department : list){
            //设置部长信息
            department.setDeptLeader(employeeMapper.selectByPrimaryKey(department.getLeader()));
        }
        page.setTotalrecord(departmentMapper.selectCountByExample(example));
        page.setRecords(list);
        result.setData(page);
        return result;
    }

    @RequestMapping("getAll")
    public JsonResult getAllDepartments(){
        JsonResult result = new JsonResult(true, 200, "查询成功");
        List<Department> list = departmentMapper.selectAll();
        result.setData(list);
        return result;
    }

    /**
     * 新增或修改部门
     * @param dept
     * @return
     */
    @RequestMapping("saveOrUpdate")
    public JsonResult deptAdd(@RequestBody Department dept)  {
        JsonResult result = new JsonResult(true, 200, "操作成功");
        logger.info("新增部门");
        if(dept.getDeptId() == null){
            departmentMapper.insertSelective(dept);
        }else{
            departmentMapper.updateByPrimaryKeySelective(dept);
        }
        return result;
    }

    @RequestMapping("info/{deptId}")
    public JsonResult getDeptInfo(@PathVariable Integer deptId) {
        JsonResult result = new JsonResult(true, 200, "查询成功");
        Department dept = departmentMapper.selectByPrimaryKey(deptId);
        result.setData(dept);
        return result;
    }
}
